<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_detail.css">
    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css">
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.min.js"></script>
</head>

<body>
    <!-- 这是导航栏 -->
    <div class="nav">
        <img src="image/log.jpg" alt="">
        <span>我的博客系统</span>
        <!-- 空白元素, 用来占位置 -->
        <div class="spacer"></div>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
    </div>
    <!-- 这里的 .container 作为页面的版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="left">
            <!-- 表示整个用户信息区域. -->
            <div class="card">
                <img src="image/head.jpg" alt="">
                <h3></h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="right">
            <!-- 使用这个 div 来包裹整个博客的内容详情 -->
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3> </h3>
                <!-- 博客的时间 -->
                <div class="date"> </div>
                <!-- 正文 -->
                <div id="content" style="opacity: 80%;">

                </div>
            </div>
        </div>
    </div>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <!-- <script src="js/commom.js"></script> -->
    <script>
        function getBlogDetail() {
            $.ajax({
                type: 'get',
                url: '/blog/getblogbyid' + location.search,
                success: function (body) {
                    //构造标题
                    let h3 = document.querySelector(".blog-content>h3");
                    h3.innerHTML = body.title;
                    //构造时间
                    let dateDiv = document.querySelector(".date");
                    dateDiv.innerHTML = body.postTime;
                    //构造正文
                    // let contentDiv = document.querySelector(".content");
                    // contentDiv.innerHTML = body.content;
                    editormd.markdownToHTML('content', {
                        markdown: body.content
                    });
                }
            })
        }
        getBlogDetail();



        //用于检验当前状态是否是登陆
        function getUserInfo(pageName) {
            $.ajax({
                type: 'get',
                url: '/user/loginin',
                success: function (body) {
                    if (body.userId && body.userId > 0) {
                        console.log("当前用户登录成功!用户名: " + body.username);
                        // if (pageName == "blog_list.html") {
                        //     ChangeUserName(body.username);
                        // }
                        //回调后再调用拿到作者信息(控制ajax的执行顺序,因为是异步操作)
                        getAuthorInfo(body)
                    } else {
                        alert('当前你未登陆,请登录后再操作')
                        location.assign('blog_login.html');
                    }
                },
                error: function () {
                    alert('当前你未登陆,请登录后再操作')
                    location.assign('blog_login.html');
                }
            })
        }

        function ChangeUserName(username) {
            let h3 = document.querySelector('.card>h3');
            h3.innerHTML = username;
        }

        // 从服务器获取当前博客的作者信息,并且显示到界面上
        // 这里的user从服务器拿到的登陆用户信息
        function getAuthorInfo(user) {
            $.ajax({
                type: 'get',
                url: '/user/authorInfo' + location.search,
                //此处的body就是一个user信息(博客作者信息)
                success: function (body) {
                    console.log("到了")
                    if (body.username) {
                        ChangeUserName(body.username);
                        if(body.username == user.username) {
                            //作者和登陆用户是同一个,显示一个删除博客按钮
                            let navDiv = document.querySelector('.nav');
                            let a = document.createElement('a');
                            a.innerHTML = "删除";
                            //构造出blogDelete?blogId=1;
                            a.href = 'blogDelete'+location.search;
                            navDiv.appendChild(a);
                        }
                    } else {
                        console.log('获取作者信息失败' + body.reason);
                    }
                }
            })
        }

        getUserInfo("blog_detail.html");
    </script>


</body>

</html>